package com.sdp.right.db.sys_flow_node_attribute_version.service.impl;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.right.db.sys_flow_node_attribute_version.mapper.SysFlowNodeAttributeVersionMapper;
import com.sdp.right.db.sys_flow_node_attribute_version.service.ISysFlowNodeAttributeVersionService;
import com.sdp.right.entity.SysFlowNodeAttributeVersion;
import com.sdp.right.entity.SysFlowTemplateVersion;
import com.sdp.right.service.RemoveSystemService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-07-17
 */
@Service
public class SysFlowNodeAttributeVersionServiceImpl extends ServiceImpl<SysFlowNodeAttributeVersionMapper, SysFlowNodeAttributeVersion> implements ISysFlowNodeAttributeVersionService,RemoveSystemService {

	@Override
	public void createFlowNodeAttributeVersion(SysFlowTemplateVersion sysFlowTemplateVersion) {
		JSONObject attributeObj = JSONObject.parseObject(new String(Base64.getDecoder().decode(sysFlowTemplateVersion.getTemplateAttribute())));
		List<SysFlowNodeAttributeVersion> list = new ArrayList<>();
		for (String key : attributeObj.keySet()) {
            JSONObject val = attributeObj.getJSONObject(key);
            SysFlowNodeAttributeVersion attribute = new SysFlowNodeAttributeVersion();
            attribute.setNodeNo(val.getString("id"));
            attribute.setNodeType(val.getString("type"));
            attribute.setNodeLabel(val.getString("label"));
            attribute.setNodeHandleMode(val.getString("handleMode"));
            attribute.setNodePassMode(val.getString("passMode"));
            attribute.setNodeAssigneeMode(val.getString("assigneeMode"));
            attribute.setNodeAssigneeEmpName(val.getString("assigneeEmpName"));
            attribute.setNodeAssigneeEmpNo(val.getString("assigneeEmpNo"));
            attribute.setNodeAssigneeRole(val.getString("assigneeRole"));
            attribute.setSystemNo(sysFlowTemplateVersion.getSystemNo());
            attribute.setTemplateNo(sysFlowTemplateVersion.getTemplateNo());
            attribute.setTemplateVersion(sysFlowTemplateVersion.getTemplateVersion());
            if(!StringUtils.isEmpty(val.getString("templateForm"))) {
            	attribute.setTemplateForm(val.getString("templateForm"));
            }else {
            	attribute.setTemplateForm(sysFlowTemplateVersion.getTemplateForm());
            }
            
            if(!StringUtils.isEmpty(val.getString("pageId"))) {
            	attribute.setPageId(val.getString("pageId"));
            }else {
            	attribute.setPageId(sysFlowTemplateVersion.getPageId());
            }
            attribute.setNodeBeforeExecution(val.getString("beforeExecution"));
            attribute.setNodeAfterExecution(val.getString("afterExecution"));
            
            list.add(attribute);
        }
		saveBatch(list);
		
	}

	@Override
	public void clearHisFlowNodeAttributeVersion(String version, String templateNo) {
		LambdaQueryWrapper<SysFlowNodeAttributeVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.ne(SysFlowNodeAttributeVersion::getTemplateVersion, version);
		queryWrapper.eq(SysFlowNodeAttributeVersion::getTemplateNo, templateNo);
		remove(queryWrapper);
		
	}

	@Override
	public SysFlowNodeAttributeVersion queryFlowNodeAttributeVersion(String version, String templateNo,String nodeNo) {
		LambdaQueryWrapper<SysFlowNodeAttributeVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowNodeAttributeVersion::getTemplateVersion, version);
		queryWrapper.eq(SysFlowNodeAttributeVersion::getTemplateNo, templateNo);
		queryWrapper.eq(SysFlowNodeAttributeVersion::getNodeNo, nodeNo);
		return getOne(queryWrapper);
		
	}

	@Override
	public void removeFlowNodeAttributeVersion(String templateNo) {
		LambdaQueryWrapper<SysFlowNodeAttributeVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowNodeAttributeVersion::getTemplateNo, templateNo);
		remove(queryWrapper);
	}

	@Override
	public void removeBySystemNo(String systemNo) {
		LambdaQueryWrapper<SysFlowNodeAttributeVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowNodeAttributeVersion::getSystemNo, systemNo);
		remove(queryWrapper);
		
	}

}
